package com.infore.operation.controller;

import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.infore.operation.entity.Equip;
import com.infore.operation.entity.EquipLog;
import com.infore.operation.entity.EquipReason;
import com.infore.operation.service.UserService;
import com.infore.operation.utils.AbstractJsonObject;
import com.infore.operation.utils.EquipEncrypt;
import com.infore.operation.utils.FormatUtil;
import com.infore.operation.utils.StatusCode;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;

@RestController
public class AuthorityController {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 获取申请理由列表成功
	 * @param userId
	 * @param equipId
	 * @return
	 */
	@RequestMapping("/servlet/user/getEquipReason")
	public AbstractJsonObject getEquipReason(@RequestParam("modelId")Integer modelId){
		List<EquipReason> list = userService.queryReason(modelId);
		Map<String, Object> map = new HashMap<String, Object>();
        map.put("beanList", list);
        return FormatUtil.queryListObject(map,"获取申请理由列表成功!");
	} 
	
	/**
	 * 提交密码申请
	 * @param userId
	 * @param equipId
	 * @return
	 */
	@RequestMapping("/servlet/user/submitPasswordApply")
	public AbstractJsonObject submitPasswordApply(@RequestParam("equipCode")String equipCode,
			@RequestParam("userId")Integer userId,@RequestParam("modelId")Integer modelId,
			@RequestParam("applyDate")String applyDate,@RequestParam("reason")String reason){
		
		if(equipCode==null||"".equals(equipCode)) {
			 return FormatUtil.queryAbstractJsonObject("请完善设备ID,否则无法生成访问密码！",StatusCode.CODE_ERROR);
		}
		
		int count = userService.queryPsonCountByUserId(userId);
		if(count>0) {
			return FormatUtil.queryAbstractJsonObject("您没有提交密码申请权限，请联系管理员！",StatusCode.CODE_ERROR);
		}
		
		String msg1 = equipCode.substring(0, equipCode.length()-9);
		int type = 0;
		if("YX-NH3-N-Ⅲ".equals(msg1)) {
			type = 1;
		}else if("YX-TP".equals(msg1)) {
			type = 2;
		}else if("YX-CODcr-Ⅱ".equals(msg1)) {
			type = 3;
		}else if("YX-CODmn".equals(msg1)) {
			type = 4;
		}else if("YX-TN".equals(msg1)) {
			type = 5;
		}else if("YX-SAMS".equals(msg1)) {
			type = 51;
		}else if("YX-CMDS".equals(msg1)) {
			type = 52;
		}else if("YX-CAMS".equals(msg1)) {
			type = 53;
		}else if("YX-NAMS".equals(msg1)) {
			type = 54;
		}else if("YX-OAMS".equals(msg1)) {
			type = 55;
		}else if("YX-PM2.5".equals(msg1)) {
			type = 56;
		}else if("YX-PM10".equals(msg1)) {
			type = 57;
		}else if("YX-MCMS".equals(msg1)) {
			type = 58;
		}else if("YX-GUV-I".equals(msg1)) {
			type = 101;
		}else if("YX-CEMS".equals(msg1)) {
			type = 102;
		}else {
			return FormatUtil.queryAbstractJsonObject("该型号不支持生成密码！",StatusCode.CODE_ERROR);
		}
		
		List<EquipReason> list = userService.queryReason1(modelId,reason);
		if(list==null||list.size()<=0) {
			EquipReason reason2 = new EquipReason();
			reason2.setModelId(modelId);
			reason2.setReason(reason);
			userService.insertEquipReason(reason2);
		}
		
		
		
		int equipmentType = type;
		int equipmentId=Integer.valueOf(equipCode.substring(equipCode.lastIndexOf("-")+1));
		int userId1=userId;
		int year=Integer.valueOf(applyDate.substring(0, 4));
		int month=Integer.valueOf(applyDate.substring(5, 7));
		int day=Integer.valueOf(applyDate.substring(8, 10));
		int hour=Integer.valueOf(applyDate.substring(11, 13));
		
		Pointer p = new Memory(16) ;
		EquipEncrypt.instanceDll.Access_Encrypted(p,equipmentType,equipmentId,userId1,year,month,day,hour);
	
		byte[] str = p.getByteArray(0, 16);
		String msg = null;
		try {
			msg = new String(str,"GB2312");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		EquipLog equipLog = new EquipLog();
		equipLog.setUserId(userId);
		equipLog.setEquipId(equipId);
		equipLog.setApplyDate(applyDate);
		equipLog.setReason(reason);
		equipLog.setEquipCode(equipCode);
		equipLog.setPassword(msg);
		userService.insertEquipLog(equipLog);
		
        return FormatUtil.queryAbstractJsonObject(msg,StatusCode.CODE_SUCCESS);
	} 
	
	@RequestMapping("/initEquipId")
	public void initEquipId() {
		List<Equip> list = userService.queryAllEquip();
		List<Equip> list1 = new ArrayList<>();
		Calendar calendar = Calendar.getInstance();
		calendar.set(2016, 0, 1);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		int count = 1;
		int num = 1;
		int sum = 1;
		if(list!=null&&list.size()>0) {
			for(Equip equip : list) {
				String model = equip.getModelname();
				String time = sdf.format(calendar.getTime());
				String xh = "";
				if(count<10) {
					xh="0"+count;
				}else {
					xh=count+"";
				}
				String equipId = model+"-"+time.substring(2).replaceAll("-", "")+xh;
				count++;
				if(count>99) {
					count = 1;
					calendar.add(Calendar.DATE, 1);
				}
				Timestamp timestamp = null;
				try {
					timestamp = new Timestamp(sdf.parse(time).getTime());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				equip.setProduceT(new Date(timestamp.getTime()));
				equip.setIdc(equipId);
				list1.add(equip);
				num++;
				if(num>500) {
					userService.updateAllEquip(list1);
					list1 = new ArrayList<>();
					num=1;
				}
				if(sum==list.size()) {
					userService.updateAllEquip(list1);
				}
				sum++;
				
			}
		}
	
	}
	

}
